<template>
  <el-form ref="mapForm" :model="mapForm" label-width="120px">
    <el-row :gutter="24">
      <el-col :span="24">
        <el-button
          type="primary"
          :style="{ float: 'right', marginBottom: '10px' }"
          @click="dataFormSubmit()"
          >保存{{ activeName }}</el-button
        >
      </el-col>
      <el-col :span="12">
        <el-form-item label="地图选择" prop="mapCode">
          <el-select
            v-model="mapForm.mapCode"
            filterable
            placeholder="地图选择"
            clearable
            :style="{ width: '100%' }"
            @change="changeMap($event)"
          >
            <el-option
              v-for="item in mapList"
              :key="item.mediaCode"
              :label="item.mediaName"
              :value="item.mediaCode"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="选边队伍" prop="pickSideTeamId">
          <el-select
            v-model="mapForm.pickSideTeamId"
            filterable
            placeholder="选边队伍"
            clearable
            :style="{ width: '100%' }"
            @change="changeTeam($event)"
          >
            <el-option
              v-for="item in matchTeamsList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="地图预览" prop="mapPicUrl">
          <img
            :src="mapForm.mapPicUrl"
            width="283"
            height="91"
            v-if="mapForm.mapPicUrl"
          />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="队伍LOGO" prop="pickSideTeamLogo">
          <img
            :src="mapForm.pickSideTeamLogo"
            width="63"
            height="91"
            v-if="mapForm.pickSideTeamLogo"
          />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="比赛状态" prop="matchStatus">
          <el-select
            v-model="mapForm.matchStatus"
            placeholder="比赛状态"
            filterable
            clearable
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="item in matchStatusData"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <hr />
        <el-form-item label="上半场"></el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="左边小局计分" prop="roundOneLeftTeamScore">
          <el-input-number
            type="number"
            :min="0"
            :style="{ width: '100%' }"
            v-model="mapForm.roundOneLeftTeamScore"
            placeholder="左边小局计分"
          >
          </el-input-number>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="右边小局计分" prop="roundOneRightTeamScore">
          <el-input-number
            v-model="mapForm.roundOneRightTeamScore"
            placeholder="右边小局计分"
            :style="{ width: '100%' }"
            :min="0"
          ></el-input-number>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="手枪局获胜" prop="roundOneHgWinId">
          <el-select
            v-model="mapForm.roundOneHgWinId"
            filterable
            placeholder="手枪局获胜"
            clearable
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="item in matchTeamsList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>

      <el-col :span="24">
        <hr />
        <el-form-item label="下半场"></el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="左边小局计分" prop="roundTwoLeftTeamScore">
          <el-input-number
            type="number"
            :min="0"
            :style="{ width: '100%' }"
            v-model="mapForm.roundTwoLeftTeamScore"
            placeholder="左边小局计分"
          >
          </el-input-number>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="右边小局计分" prop="roundTwoRightTeamScore">
          <el-input-number
            v-model="mapForm.roundTwoRightTeamScore"
            placeholder="右边小局计分"
            :style="{ width: '100%' }"
            :min="0"
          ></el-input-number>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="手枪局获胜" prop="roundTwoHgWinId">
          <el-select
            v-model="mapForm.roundTwoHgWinId"
            filterable
            placeholder="手枪局获胜"
            clearable
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="item in matchTeamsList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>

      <el-col :span="12">
        <el-form-item label="是否加时" prop="overtimeFlag">
          <el-switch
            v-model="mapForm.overtimeFlag"
            active-value="Y"
            inactive-value="N"
            :style="{ width: '100%' }"
          >
          </el-switch>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row v-if="mapForm.overtimeFlag === 'Y'">
      <el-col :span="24">
        <hr />
        <el-form-item label="加时"></el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="左边小局计分" prop="overtimeLeftTeamScore">
          <el-input-number
            type="number"
            :min="0"
            :style="{ width: '100%' }"
            v-model="mapForm.overtimeLeftTeamScore"
            placeholder="左边小局计分"
          >
          </el-input-number>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="右边小局计分" prop="overtimeRightTeamScore">
          <el-input-number
            v-model="mapForm.overtimeRightTeamScore"
            placeholder="右边小局计分"
            :style="{ width: '100%' }"
            :min="0"
          ></el-input-number>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>

<script>
import { request } from "@/utils/axios-util";
import { matchStatusData } from "../config";

export default {
  props: {
    mapFormInit: {
      type: Object,
      default: () => ({}),
    },
    activeName: {
      type: String,
      default: "",
    },
    leagueCode: {
      type: String,
      default: "",
    },
    teamList: {
      type: Array,
      default: () => [],
    },
    mapList: {
      type: Array,
      default: () => [],
    },
    matchTeamsList: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      matchStatusData,
      mapForm: {},
    };
  },
  methods: {
    changeMap(e) {
      let obj = { mediaUrl: "" };
      if (e) {
        obj = this.mapList.find((item) => item.mediaCode == e);
      }

      this.mapForm = {
        ...this.mapForm,
        mapPicUrl: obj.mediaUrl,
      };
    },
    changeTeam(e) {
      let obj = { logoUrl: "" };
      if (e) {
        obj = this.teamList.find((item) => item.id == e);
      }

      this.mapForm = {
        ...this.mapForm,
        pickSideTeamLogo: obj.logoUrl,
      };
    },
    // 更新基础数据
    dataFormSubmit() {
      const data = {
        ...this.mapForm,
      };
      this.$refs.mapForm.validate((valid) => {
        if (valid) {
          const url = "/admin/match/updateOne";
          request(url, data, "POST").then(
            (res) => {
              if (res.code === 200) {
                this.$message.success("操作成功");
              } else {
                this.$message.error(res.msg);
              }
            },
            (resp) => {
              this.$message.error("操作失败，", resp);
            }
          );
        }
      });
    },
  },
  watch: {
    mapFormInit(newVal, oldVal) {
      this.mapForm = { ...newVal };
      this.changeTeam(newVal.pickSideTeamId);
    },
    immediate: true,
  },
};
</script>

<style scoped>
hr {
  background-color: #ccc;
  height: 1px;
  border: none;
}
</style>
